package com.dhecp.project.common.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhecp.common.constant.Constants;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.framework.shiro.service.SysPasswordService;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.system.domain.SysUserInfo;
import com.dhecp.project.system.service.SysUserInfoService;

/**
 * 密码验证
 * @author DH-Xxj
 * @version 2020年7月17日
 */
@Controller
@RequestMapping("/common/password")
public class SysPasswordController extends BaseController{

    @Autowired
    private SysUserInfoService userService;
    
    @Autowired
    private SysPasswordService passwordService;
    
    /**
     * 验证用户密码
     * @param matchType 验证类型：（userpassword：验证登录密码，userpassword2：验证二级密码）
     * @param userId
     * @param pwd
     * @return AjaxResult
     * @author DH-Xxj
     */
    @PostMapping("/matchepassword")
    @ResponseBody
    public AjaxResult matchePassword(@RequestParam Map<String, Object> params){
    	AjaxResult ajaxResult = new AjaxResult();
    	String userId = "";
    	String pwd = "";

    	if (params.get("pwd") == null){
    		pwd = "";
    	}else{
    		pwd = params.get("pwd").toString();
    	}
    	
    	if (params.get("matchType") == null){
    		ajaxResult = AjaxResult.error("缺少验证类型（matchType），userpassword：验证登录密码，userpassword2：验证二级密码");
    	}else{
    		SysUserInfo userInfo = new SysUserInfo();
    		if ("userpassword".equals(params.get("matchType").toString().toLowerCase())){ //验证登录密码
    			userId = ShiroUtils.getUserId();
    	    	userInfo = userService.queryByUseridAndPass(userId, ""); //查询用户信息
    	    	if (userInfo.getUserPassWord().equals(passwordService.encryptPassword(userInfo.getUserId(), pwd, Constants.PASSWORD_SALT))){
					if (params.get("pwd").toString().equals(userInfo.getUserId())) ajaxResult = AjaxResult.error("不允许使用默认密码，请先修改登录密码。");
					else ajaxResult = AjaxResult.success("身份验证通过。");
    	    	}else{
    	    		ajaxResult = AjaxResult.error("登录密码错误。");
    	    	}
    		}else if ("userpassword2".equals(params.get("matchType").toString().toLowerCase())){ //验证二级密码
    			userId = ShiroUtils.getMandatorId();
    	    	userInfo = userService.queryByUseridAndPass(userId, ""); //查询用户信息
    	    	String userPassWord = userInfo.getUserPassWord();
    	    	String userPassWord2 = userInfo.getUserPassWord2();
    	    	if (userPassWord2 == null || "".equals(userPassWord2)){
    	    		ajaxResult = AjaxResult.error("二级密码未设置，请先设置二级密码。");
    	    	}else if (userPassWord2.equals(userPassWord)){
    	    		ajaxResult = AjaxResult.error("二级密码不能与登陆密码相同，请重新设置二级密码。");
    	    	}else if (userInfo.getUserPassWord2().equals(passwordService.encryptPassword(userInfo.getUserId(), pwd, Constants.PASSWORD_SALT))){
					if (params.get("pwd").toString().equals(userInfo.getUserId())) ajaxResult = AjaxResult.error("不允许使用默认密码，请先修改二级密码。");
					else ajaxResult = AjaxResult.success("身份验证通过。");
    	    	}else{
    	    		ajaxResult = AjaxResult.error("二级密码错误。");
    	    	}
    		}else{
    			ajaxResult = AjaxResult.error("验证类型（matchType）无效，可选类型（userpassword：验证登录密码，userpassword2：验证二级密码）");
    		}
    	}
    		
    	return ajaxResult;
    }
    
}
